package cui.xin.studentcourse.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import cui.xin.studentcourse.bean.Course;
import cui.xin.studentcourse.bean.Middle;
import cui.xin.studentcourse.bean.Student;
import cui.xin.studentcourse.dto.ResponseBean;
import cui.xin.studentcourse.service.CourseService;
import cui.xin.studentcourse.service.MiddleService;
import cui.xin.studentcourse.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
/**
 * 课程表 课程管理
 * @author palpitate_yzr
 * @since 2022年11月16日16:36:46
 */
@RestController
@RequestMapping(value = "/Course")
@Api(tags = "管理课程")
@Slf4j
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private MiddleService middleService;

    @Autowired
    private StudentService studentService;

    /**
     * auth：yangbx
     * 学生根据条件查询课程
     * @param condition
     * @return
     */
    @RequestMapping("/queryCourse")
     @ApiOperation(value = "模糊查询已选课程",notes = "学生根据条件模糊查询已选课程")
    public ResponseBean queryCourseByCondition(@RequestBody(required = false) String condition,HttpSession session) {
        ResponseBean responseBean = new ResponseBean();
        //将字符串转换成json
        JSONObject jsonObject = JSONObject.parseObject(condition);
        String s = jsonObject.getString("condition");
        Long studentId = (Long) session.getAttribute("studentId");
        List<Course> courses = courseService.queryCourseByCondition(s,studentId);
        responseBean.setContent(courses);
        return responseBean;
    }


    @ApiOperation(value = "模糊查询已选课程",notes = "学生根据条件模糊查询已选课程")
    @RequestMapping("/queryCourseByString")
    public ResponseBean queryCourseByString(@RequestBody(required = false) String condition,HttpSession session) {
        log.info("condition:"+condition);
        ResponseBean responseBean = new ResponseBean();

        //将字符串转换成json
        JSONObject jsonObject = JSONObject.parseObject(condition);
        String s = jsonObject.getString("condition");
        Long studentId = (Long) session.getAttribute("studentId");
        List<Course> courses = courseService. selectListByCondition(s,studentId);
        responseBean.setContent(courses);
        System.out.println(courses);
        return responseBean;
    }

    @RequestMapping("/delCourse")
    @ApiOperation(value = "退课",notes = "学生退课")
    public ResponseBean delCourse(@RequestBody String courseId,HttpSession session){
        Middle middle=new Middle();
        Long studentId = (Long) session.getAttribute("studentId");
        middle.setStudentId(studentId);
        middle.setCourseId(Long.valueOf(courseId));
        int result=middleService.delCourse(middle);
        if (result<1){
            return new ResponseBean<>(302,"服务器错误");
        }
        return new ResponseBean<>(result);
    }

    /**
     * 选课页面 查询课程 根据当前登录的学生对应没有的课程号查询
     * @return
     */
    @GetMapping("/queryList")
    public List<Course> selectList(HttpSession session){
        Long studentId = (Long) session.getAttribute("studentId");
        return courseService.selectCourseList(studentId);
    }


    @RequestMapping(value = "/addCourse" )
    @ApiOperation(value = "选课",notes = "添加课程")
    public void addCourse(@RequestBody String courseId,HttpSession session){
        Middle middle=new Middle();
        System.out.println("courseId："+courseId);
        log.info(String.valueOf(courseId));
        Course course = courseService.selectCourseById(Long.valueOf(courseId));
        BeanUtil.copyProperties(course,middle);
        Long studentId = (Long) session.getAttribute("studentId");
        Student student = studentService.queryStudentByStudentId(studentId);
        BeanUtil.copyProperties(student,middle);
        courseService.addCourse(middle);
    }

    @RequestMapping("/edit")
    public void edit(Course course){
        courseService.editCourse(course);
    }

    @RequestMapping("/query")
    public Course query(Integer id){
        return courseService.queryCourseById(id);
    }

    @RequestMapping("/del/{id}")
    public void del(@PathVariable("id") Integer id){
        courseService.delCourseById(id);
    }



    @RequestMapping("/top10")
    public List<Course> top10(){
        return courseService.selectTop10();
    }

    @RequestMapping("/sex")
    public List<Course> sex(){
        return courseService.selectSex();
    }
}
